import {supportFunction} from "../../utils/supportFunction";
import moduleInstanceManager from "../managers/ModuleInstanceManager";
import {ConstVariable} from "./ConstVariable";

const PresetModuleSetting = {
    'classroom_objective_simple': {
        controller: {},
        layout: "",
        functional: []
    },
    'classroom_objective_stander': {
        controller: {},
        layout: "",
        functional: []
    },
    'classroom_subjective_simple': {
        controller: {},
        layout: "",
        functional: []
    },
    'classroom_subjective_stander': {
        controller: {},
        layout: "",
        functional: []
    },
    'exercise_simple': {
        controller: {},
        layout: "",
        functional: []
    },
    'exercise_stander': {
        controller: {},
        layout: "",
        functional: []
    }
};

class ModuleLoader {
    constructor(launcher, provider) {
        this._launcher = launcher;
        this._provider = provider;
        moduleInstanceManager.init(this._launcher);
    }

    load(config, model) {
        //优先加载布局组件
        if (typeof config.layout !== 'string') {
            throw Error('Invalid layout component configuration');
        }
        //加载布局组件
        this._loadModule(config.layout, model[config.layout], {zindex: 200})
            .then(instance => {

            });
    }

    unload() {
        moduleInstanceManager.remove(this._launcher);
    }

    _loadControllers() {

    }

    _loadComponents() {

    }

    _afterLoadLayout(instance) {

    }

    _loadModule(key, model, options) {
        this._provider.load(key, model, options)
            .then((instance) => {
                let service = instance.getService && instance.getService() || {};
                /*return ($.isFunction(service.then) ? $.Deferred(function (_defer) {
                    service.then(function () {
                        _defer.resolve(instance._service_);
                    }).promise() : service;
                }));*/


                let instanceType = ConstVariable.PresenterType.FUNCTIONAL;
                if (supportFunction.hasProp(instance, 'metaInfo')) {
                    instanceType = instance.metaInfo.type;
                }
                //根据模块的类型进行处理
                switch (instanceType) {
                    //对控制器的处理
                    case ConstVariable.PresenterType.CONTROLLER:
                        let modules = moduleInstanceManager.get(this._launcher);
                        //功能组件
                        if (supportFunction.hasProp(modules, ConstVariable.PresenterType.FUNCTIONAL)) {
                            modules[ConstVariable.PresenterType.FUNCTIONAL].forEach(module => {
                                instance['processBeforeRun'].call(module);
                            });
                        }
                        //布局组件
                        if (supportFunction.hasProp(modules, ConstVariable.PresenterType.LAYOUT)) {
                            modules[ConstVariable.PresenterType.LAYOUT].forEach(module => {
                                instance['processBeforeRun'].call(module);
                            });
                        }
                        //控制组件
                        if (supportFunction.hasProp(modules, ConstVariable.PresenterType.CONTROLLER)) {
                            modules[ConstVariable.PresenterType.CONTROLLER].forEach(module => {
                                instance['processBeforeRun'].call(module);
                            });
                        }
                        //将本体加入到管理对象中
                        moduleInstanceManager.add(instance);
                        break;
                    case ConstVariable.PresenterType.LAYOUT:
                        let service = instance.getService && instance.getService() || {};
                        return ($.isFunction(service.then))
                    case ConstVariable.PresenterType.FUNCTIONAL:
                        moduleInstanceManager.add(instance);
                        break;
                }
            });
    }

    runOver(metaInfo) {
        const key = ModuleLoader.getInstanceKey(metaInfo);
        const moduleInstance = ModuleInstanceManager.getInstance()
            .getModuleInstance(this._launcher, metaInfo.type, key);
        //获取所有的控制器实例
        const controllers = moduleInstanceManager.get(this._launcher, ConstVariable.PresenterType.CONTROLLER);
        controllers.forEach(instance => {
            if (instance['processAfterRun']) {
                instance['processAfterRun'].call(moduleInstance);
            }
        });
    }

    static getPresetConfig(name) {
        if (!supportFunction.hasProp(PresetModuleSetting, name)) {
            throw Error('Can not find the specified configuration');
        }
        return PresetModuleSetting[name];
    }

    static getInstanceKey(metaInfo) {
        return (metaInfo.name ? metaInfo.name : 'None') + '@' + (metaInfo._CLASS_ID_ ? metaInfo._CLASS_ID_ : '0');
    }
}

export {ModuleLoader};